package com.zlq.cn.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;
import java.util.List;

@Configuration
//@Profile("dev")
@EnableSwagger2WebMvc
public class Knife4jConfiguration {

    @Bean(value = "fanyiApi")
    public Docket fanyiApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("翻译")
                .globalOperationParameters(getGlobalOperationParameters())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zlq.cn.controller.sign.fanyi"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean(value = "movieApi")
    public Docket movieApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("影片")
                .globalOperationParameters(getGlobalOperationParameters())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zlq.cn.controller.sign.movie"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean(value = "loversApi")
    public Docket loversApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("小情侣")
                .globalOperationParameters(getGlobalOperationParameters())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zlq.cn.controller.sign.lovers"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean(value = "dzmyApi")
    public Docket dzmyApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("电子木鱼")
                .globalOperationParameters(getGlobalOperationParameters())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zlq.cn.controller.sign.dzmy"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean(value = "wnxsApi")
    public Docket wnxsApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("为你写诗")
                .globalOperationParameters(getGlobalOperationParameters())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zlq.cn.controller.sign.wnxs"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean(value = "commonApi")
    public Docket commonApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("公共api")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zlq.cn.controller"))
                .paths(s -> s.startsWith("/api/common"))
                .build();
    }

    @Bean(value = "adminApi")
    public Docket adminApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("管理端api")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zlq.cn.admin.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean(value = "aiApi")
    public Docket chatgptApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("AI")
                .globalOperationParameters(getGlobalOperationParameters())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zlq.cn.controller.sign.ai"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean(value = "juheApi")
    public Docket juheApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("聚合")
                .globalOperationParameters(getGlobalOperationParameters())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zlq.cn.controller.sign.juhe"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean(value = "exam")
    public Docket examApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("题库")
                .globalOperationParameters(getGlobalOperationParameters())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zlq.cn.controller.sign.exam"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean(value = "driving")
    public Docket drivingApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("驾考")
                .globalOperationParameters(getGlobalOperationParameters())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zlq.cn.controller.sign.driving"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .description("接口文档")
                .version("1.0")
                .build();
    }

    private static List<Parameter> getGlobalOperationParameters() {
        List<Parameter> pars = new ArrayList<>();
        ParameterBuilder parameterBuilder = new ParameterBuilder();
        parameterBuilder.name("packageName")
                .description("应用包名")
                .modelRef(new ModelRef("string"))
                .parameterType("body")
                .required(true);
        pars.add(parameterBuilder.build());
        parameterBuilder.name("version")
                .description("应用版本号")
                .modelRef(new ModelRef("string"))
                .parameterType("body")
                .required(true);
        pars.add(parameterBuilder.build());
        parameterBuilder.name("timestamp")
                .description("时间戳")
                .modelRef(new ModelRef("long"))
                .parameterType("body")
                .required(true);
        pars.add(parameterBuilder.build());
        parameterBuilder.name("sign")
                .description("签名结果")
                .modelRef(new ModelRef("string"))
                .parameterType("body")
                .required(true);
        pars.add(parameterBuilder.build());
        parameterBuilder.name("imei")
                .description("唯一标识")
                .modelRef(new ModelRef("string"))
                .parameterType("body")
                .required(true);
        pars.add(parameterBuilder.build());
        return pars;
    }
}
